﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web.Http.Description;
using DAL;

namespace WebAPI.Controllers
{
    public class EquipeEvenementController : ApiController
    {
        private MyTeamEntities db = new MyTeamEntities();

        // GET api/EquipeEvenement
        public IQueryable<EquipeEvenement> GetEquipeEvenements()
        {
            return db.EquipeEvenements;
        }

        // GET api/EquipeEvenement/5
        [ResponseType(typeof(EquipeEvenement))]
        public IHttpActionResult GetEquipeEvenement(int id)
        {
            EquipeEvenement equipeevenement = db.EquipeEvenements.Find(id);
            if (equipeevenement == null)
            {
                return NotFound();
            }

            return Ok(equipeevenement);
        }

        // PUT api/EquipeEvenement/5
        public IHttpActionResult PutEquipeEvenement(int id, EquipeEvenement equipeevenement)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            if (id != equipeevenement.Equ_Id)
            {
                return BadRequest();
            }

            db.Entry(equipeevenement).State = EntityState.Modified;

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!EquipeEvenementExists(id))
                {
                    return NotFound();
                }
                else
                {
                    throw;
                }
            }

            return StatusCode(HttpStatusCode.NoContent);
        }

        // POST api/EquipeEvenement
        [ResponseType(typeof(EquipeEvenement))]
        public IHttpActionResult PostEquipeEvenement(EquipeEvenement equipeevenement)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            db.EquipeEvenements.Add(equipeevenement);

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateException)
            {
                if (EquipeEvenementExists(equipeevenement.Equ_Id))
                {
                    return Conflict();
                }
                else
                {
                    throw;
                }
            }

            return CreatedAtRoute("DefaultApi", new { id = equipeevenement.Equ_Id }, equipeevenement);
        }

        // DELETE api/EquipeEvenement/5
        [ResponseType(typeof(EquipeEvenement))]
        public IHttpActionResult DeleteEquipeEvenement(int id)
        {
            EquipeEvenement equipeevenement = db.EquipeEvenements.Find(id);
            if (equipeevenement == null)
            {
                return NotFound();
            }

            db.EquipeEvenements.Remove(equipeevenement);
            db.SaveChanges();

            return Ok(equipeevenement);
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }

        private bool EquipeEvenementExists(int id)
        {
            return db.EquipeEvenements.Count(e => e.Equ_Id == id) > 0;
        }
    }
}